package org.sny7.openOfflineList.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import javax.swing.SwingUtilities;
import javax.swing.UIDefaults;

import org.jvnet.substance.SubstanceLookAndFeel;
import org.jvnet.substance.fonts.FontPolicy;
import org.jvnet.substance.fonts.FontSet;

public class ConfigureManager {
	
	private static File file = new File(PathGetter.getPath() + "configure.properties");
	
	private ConfigureManager() {
		
	}
	
	public static Properties createNewConfigure() {
		Properties properties = new Properties();
		properties.setProperty("language", "English US");
		properties.setProperty("skin", "org.jvnet.substance.skin.BusinessBlueSteelSkin");
		properties.setProperty("fontFamily", "");
		properties.setProperty("fontSizeIncrease", "0");
		
		save(properties);
		
		return properties;
	}
	
	public static void loadConfigure() {
		final Properties properties = file.exists() ? load() : createNewConfigure();
		
		detectLanguageFiles();

		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				try {
					SubstanceLookAndFeel.setSkin(properties.getProperty("skin"));
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});

		String fontFamily = properties.getProperty("fontFamily");
		if (!fontFamily.equals("")) {
			int fontSizeIncrease;
			try {
				fontSizeIncrease = Integer.parseInt(properties.getProperty("fontSizeIncrease"));
			} catch(NumberFormatException e) {
				fontSizeIncrease = 0;
				e.printStackTrace();
			}
			WrapperFontSet.setFontName(fontFamily);
			WrapperFontSet.setFontSizeIncrease(fontSizeIncrease);
			final FontSet substanceCoreFontSet = SubstanceLookAndFeel
					.getFontPolicy().getFontSet("Substance", null);
			SubstanceLookAndFeel.setFontPolicy(new FontPolicy() {
				public FontSet getFontSet(String lafName, UIDefaults table) {
					return new WrapperFontSet(substanceCoreFontSet);
				}
			});
		}
		
		LanguageChanger.change(PathGetter.getPath() + "languages/" + properties.getProperty("language") + ".properties");
	}
	
	private static void detectLanguageFiles() {
		detectLanguageFile("languages/English US.properties");
		detectLanguageFile("languages/Simplified Chinese.properties");
	}
	
	private static void detectLanguageFile(String fileName) {
		File file = new File(PathGetter.getPath() + fileName);
		if (file.exists()) {
			return;
		}
		
		File languagePath = new File(PathGetter.getPath() + "languages");
		if (!languagePath.exists()){
			languagePath.mkdir();
		}
		
		InputStream inputStream = ConfigureManager.class.getResourceAsStream("/" + fileName);
		FileOutputStream fileOutputStream = null;
		try {
			fileOutputStream = new FileOutputStream(file);
			Properties properties = new Properties();
			properties.load(inputStream);
			properties.store(fileOutputStream, "OpenOfflineList Language File");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fileOutputStream.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		try {
			inputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void setConfigure(String key, String value) {
		Properties properties = file.exists() ? load() : createNewConfigure();

		if (properties.size() == 0) {
			return;
		}
		
		properties.setProperty(key, value);
		
		save(properties);
	}
	
	private static void save(Properties properties) {
		FileOutputStream fileOutputStream = null;
		try {
			fileOutputStream = new FileOutputStream(file);
			properties.store(fileOutputStream, "OpenOfflienList User Configure");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fileOutputStream != null) {
				try {
					fileOutputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}
	
	private static Properties load() {
		File file = new File(PathGetter.getPath() + "configure.properties");
		if (!file.exists()) {
			createNewConfigure();
		}
		FileInputStream fileInputStream = null;
		Properties properties = null;
		try {
			fileInputStream = new FileInputStream(file);
			properties = new Properties();
			properties.load(fileInputStream);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (fileInputStream != null) {
				try {
					fileInputStream.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		return properties;
	}

}
